Python Data Science Essentials by 2015
If the value of the outliers fraction is very small, nu will be small and the SVM algorithm will try to fit the contour of the data points. On the other hand, if the fraction is high, so will be the parameter, forcing a smoother boundary of the inliers' distributions.
Let's immediately observe the performance of this algorithm on the problem that we faced before on the Boston house price dataset:
In: from sklearn.decomposition import PCA from sklearn import preprocessing from sklearn import svm # Normalized data relative to continuous variables continuous_variables = [n for n in range(np.shape(boston.data)[1]) if n!=3] normalized_data = preprocessing.StandardScaler().fit_transform(boston.data[:,continuous_variables]) # Just for visualization purposes pick the first 5 PCA components pca = PCA(n_components=5) Zscore_components = pca.fit_transform(normalized_data) vtot = 'PCA Variance explained ' + str(round(np.sum(pca.explained_variance_ratio_),3)) # OneClassSVM fitting and estimates outliers_fraction = 0.02 # nu_estimate = 0.95 * outliers_fraction + 0.05 machine_learning = svm.OneClassSVM(kernel="rbf", gamma=1.0/len(normalized_data), degree=3, nu=nu_estimate) machine_learning.fit(normalized_data) detection = machine_learning.predict(normalized_data) outliers = np.where(detection==-1) regular = np.where(detection==1) # Draw the distribution and the detected outliers from matplotlib import pyplot as plt for r in range(1,5): a = plt.plot(Zscore_components[regular,0],Zscore_components[regular,r], 'x', markersize=2, color='blue', alpha=0.6, label='inliers') b = plt.plot(Zscore_components[outliers,0],Zscore_components[outliers,r], 'o', markersize=6,color='red', alpha=0.8, label='outliers') plt.xlabel('Component 1 ('+str(round(pca.explained_variance_ratio_[0],3))+')') plt.ylabel('Component '+str(r+1)+'('+str(round(pca.explained_variance_ratio_[r],3))+')') plt.xlim([-7,7]) plt.ylim([-6,6]) plt.legend((a[0],b[0]),('inliers','outliers'),numpoints=1,loc='best') plt.title(vtot) plt.show()